const {app,port} = require('./sqlThread.js')
const regx1 = /^port=([0-9]+)$/
const {log, logOs}= require('./utils/logger.js')
const regx2 = /^((2((5[0-5])|([0-4]\d)))|([0-1]?\d{1,2}))(\.((2((5[0-5])|([0-4]\d)))|([0-1]?\d{1,2}))){3}$/
const os = require('os');

logOs('服务区启动中...')

var portNew,arrs;

for(i in process.argv){
    if((arrs = process.argv[i].match(regx1))){
         portNew = arrs[1]
         break
     }
}

/**
 * 获取当前机器的ip地址
 */
function getIpAddress() {
  let ifaces = os.networkInterfaces()
  for (let dev in ifaces) {
      let iface = ifaces[dev]
      for (let i = 0; i < iface.length; i++) {
          let { family, address, internal } = iface[i]
          if (family === 'IPv4' && address !== '127.0.0.1' && !internal) {
              return address
          }
      }
  }
}

const wlan = getIpAddress()

const server = app.listen(portNew?portNew:port,()=>{
    let p = server._connectionKey.substring(server._connectionKey.lastIndexOf(':')+1)
    logOs(`服务器运行中,运行在:\r\n- 内网URL:http://127.0.0.1:${p}\r\n- 外网URL:http://${wlan}:${p}(如果有内网转发,请以转发后的地址为准)`)
    
}).on('error',()=>{
    logOs('服务区出现异常')
})


